package com.example.javaee_library.Controller;/**
 * @author Miao-yongzheng
 * @create 2021-12-14 20:37
 */

import cn.hutool.core.convert.Convert;
import com.example.javaee_library.Entity.Book;
import com.example.javaee_library.Service.Book_Service;
import com.example.javaee_library.dao.BookDao;
import com.example.javaee_library.dto.JsonData;
import com.example.javaee_library.exception.NoBookException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description:
 * @author: 34911
 * @time: 2021/12/14 20:37
 */
@Controller
@RequestMapping("/Customer/Admin_Book")
public class Admin_BookController {
    @Autowired
    BookDao bookDao;
    @Autowired
    Book_Service book_service;

    @PostMapping("/AddBookNum")
    public JsonData AddBookNum(@RequestParam("number") String number, @RequestParam("book_id") String book_id) {

        int back;
        Book book = bookDao.queryById(Convert.toInt(book_id));
        if (book == null) {
            throw new NoBookException("没这本书");
        } else
            back = bookDao.AddNumber(Convert.toInt(number), Convert.toInt(book_id));
        if (back > 0) {
            return JsonData.buildSuccess("插入成功");
        } else
            return JsonData.buildError("插入失败");
    }

    @ResponseBody
    @GetMapping("/DeleteBook")
    public JsonData DeleteBook(@RequestParam("book_id") String book_id, ModelAndView modelAndView) {
        int back;
        Book book = bookDao.queryById(Convert.toInt(book_id));
        if (book == null) {
            throw new NoBookException("没这本书");
        } else
            back = bookDao.DeleteBook(Convert.toInt(book_id));
        if (back > 0) {
            return JsonData.buildSuccess("删除" + book_id + "号书籍成功");
        } else
            return JsonData.buildError("删除" + book_id + "号书籍失败");
    }



    @RequestMapping(value = "/listbook", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        List<Book> list = book_service.getList();
        model.addObject("list", list);
        model.setViewName("Admin_book");
        // list.jsp + model = ModelAndView
        return model;// WEB-INF/jsp/"list".jsp
    }

    @ResponseBody
    @GetMapping("/Addbook")
    public JsonData Addbook(@RequestParam("book_id") String book_id,
                            @RequestParam("book_name") String book_name,
                            @RequestParam("book_number")String book_number)
    {
        if(book_service.AddBook(Convert.toInt(book_id),book_name,Convert.toInt(book_number)))
            return JsonData.buildSuccess("插入成功");
        else
            return JsonData.buildError("插入失败");
    }

    @ResponseBody
    @PostMapping("/Updatebook")
    public JsonData Updatebook(@RequestParam("book_id") String book_id,
                            @RequestParam("book_name") String book_name,
                            @RequestParam("book_number")String book_number)
    {
        if(book_service.Updatebook(Convert.toInt(book_id),book_name,Convert.toInt(book_number)))
            return JsonData.buildSuccess("修改成功");
        else
            return JsonData.buildSuccess("修改失败");
    }




}
